/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shopcart', [
        "ngResource",
        "ngStorage"
    ])
    //购物车
    .service('ShopcartService', ['$resource', '$localStorage', 'ShopService', 'MallAuthService', function ($resource, $localStorage, ShopService, MallAuthService) {
        var $storage = $localStorage.$default({
            name: '',
            userId: '',
            wxpay_openid: '',
            order: '',
            shopcart: {},
        });
        var shop_goods_list = $storage.shopcart;
        //新增商品到购物车
        this.addgoods = function (shopid, cid, goodsid) {
            var userId = MallAuthService.getUserid();
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid && goodsitem.goodsid == goodsid) {
                    var num = goodsitem.num;
                    if (num < 999) {
                        goodsitem.num++;
                    } else {
                        goodsitem.num = 999;
                        input_alert("超出购买上限")
                    }
                    if(userId!=''){
                        loadCart(true);
                        var cartGoods = {
                            cartid:goodsitem.cartid,
                            num:goodsitem.num,
                            skuid:goodsitem.skuid
                        }
                        MallAuthService.updateCartByCartId(cartGoods,function(){
                            loadCart(false);
                        },function(){
                            alert("同步账号失败")
                        }) ;
                    }
                }
            }
            shop_goods_list[shopid] = goods_list;
            $storage.shopcart[shopid] = goods_list;
        }

        //从购物车减少购买量
        this.removeGoods = function (shopid, cid, goodsid, num) {
            //ShopMenuService.removeGood(shopid, cid, goodsid,num);
            var userId = MallAuthService.getUserid();
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid && goodsitem.goodsid == goodsid) {
                    goodsitem.num--;
                }
                if(userId!=''){
                    loadCart(true);
                    var cartGoods = {
                        cartid:goodsitem.cartid,
                        num:goodsitem.num,
                        skuid:goodsitem.skuid
                    }
                    MallAuthService.updateCartByCartId(cartGoods,function(){
                        loadCart(false);
                    },function(){
                        alert("同步账号失败")
                    }) ;
                }
            }
            shop_goods_list[shopid] = goods_list;
            $storage.shopcart[shopid] = goods_list;
        }

        //输入大于零绑定到shop_goods_list
        this.importGoods = function (shopid, cid, goodsid, num) {
            var userId = MallAuthService.getUserid();
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for (var i = 0; i < goods_list.length; i++) {
                if (goods_list[i].cid == cid && goods_list[i].goodsid == goodsid) {
                    goodsitem = goods_list[i];
                    goodsitem.num = num;
                    if(userId!=''){
                        loadCart(true);
                        var cartGoods = {
                            cartid:goodsitem.cartid,
                            num:goodsitem.num,
                            skuid:goodsitem.skuid
                        }
                        MallAuthService.updateCartByCartId(cartGoods,function(){
                            loadCart(false);
                        },function(){
                            alert("同步账号失败")
                        }) ;
                    }
                    break;
                }
            }
            shop_goods_list[shopid] = goods_list;
            $storage.shopcart[shopid] = goods_list;
        }

        //获取全部购物清单
        this.getAllShopCarInfo = function (shops) {
            var userId = MallAuthService.getUserid();
            var all_goods_list = [];
            if(userId!=''){
                shop_goods_list = shops;
                $storage.shopcart = shop_goods_list;
            }else{
                shop_goods_list = $storage.shopcart;
            }
            for (var shopid in shop_goods_list) {
                var shopinfo = ShopService.queryShopByShopid(shopid);
                var shop_goods = {
                    shopid: shopid,
                    shop: shopinfo,
                    list: shop_goods_list[shopid],
                    //goodscontent: goodscontent
                };
                all_goods_list.push(shop_goods);
            }
            return all_goods_list;
        }

        //获取店铺购物列表
        this.getShopCarByShopid = function (shopid) {
            var goods_list = [];
            shop_goods_list = $storage.shopcart;
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            return goods_list;
        }

        //获取总价和产品总数
        this.gettotalpriceandcount = function (shopid) {
            shop_goods_list = $storage.shopcart;
            var count = 0;
            var integration = 0;
            var totalprice = 0;
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.num > 0) {
                    totalprice += goodsitem.price * goodsitem.num;
                    count += 1 * goodsitem.num;
                    integration += (goodsitem.price * goodsitem.num) / 100;
                }
            }
            return {totalprice: totalprice, count: count, integration: integration};
        };

        //根据id移除某个商品
        this.cleanById = function (shopid, cid, goodsid) {
            //ShopMenuService.cleanGoodById(shopid, cid, goodsid);
            var userId = MallAuthService.getUserid();
            var goods_list = [];
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid && goodsitem.goodsid == goodsid) {
                    if(userId!=''){
                        this.removeCartGoodsByCart(goodsitem.cartid,function(){
                        });
                    }
                    goods_list.splice(i,1);
                    break;
                }
            }
            //clean shop if empty
            if (goods_list.length == 0) {
                delete shop_goods_list[shopid];
                delete $storage.shopcart[shopid];
            } else {
                shop_goods_list[shopid] = goods_list;
                $storage.shopcart[shopid] = goods_list;
            }
        }

        //移除某个商店id下的所有商品
        this.cleanByShopId = function (shopid) {
            var userId = MallAuthService.getUserid();
            //ShopMenuService.removeShopById(shopid);
            if (shop_goods_list[shopid]) {
                //将这个商店id下面所有的商品删除
                var goods_list = shop_goods_list[shopid];
                for (var i = goods_list.length - 1; i >= 0; i--) {
                    goods_list.splice(i, 1);
                }
                delete shop_goods_list[shopid]; //将这个商店id下面所有的商品删除
                $storage.shopcart = shop_goods_list;
            }
        }

        //获取商品购买数量
        this.getBuycountByGoodsid = function (shopid, cid, goodsid) {
            var goods_list = [];
            shop_goods_list = $storage.shopcart;
            if (shop_goods_list[shopid]) {
                goods_list = shop_goods_list[shopid];
            }
            for (var i = 0; i < goods_list.length; i++) {
                var goodsitem = goods_list[i];
                if (goodsitem.cid == cid && goodsitem.goodsid == goodsid) {
                    return goodsitem.num;
                }
            }
            return 0;
        }

        //同步本地缓存和购物车数据
        this.synchronizationStorage = function () {
            $storage.shopcart = shop_goods_list;
        }
        //删除购物车内容
        this.removeCartGoodsByCart = function (cartid,success) {
            var removeCartGoods = $resource(mallorder + "cart/:id");
            return removeCartGoods.remove({id: cartid},success);
        }
    }])
